# !/usr/bin/env python
# -*-coding:utf-8 -*-
"""
# File       : group_view.py
# Time       ：2023/7/9 16:39
# Author     ：Y-aong
# version    ：python 3.7
# Description：
    群组视图
    group views
"""
from flask import request
from flask_restx import fields

from apis.system_oauth import system_oauth_ns
from apis.system_oauth.models import SystemGroup, SystemGroupPermissionRelation
from apis.system_oauth.schema.group_schema import SystemGroupSchema
from public.api_utils.api_decorator import handle_api_error
from public.base_model import db
from public.base_response import MarshalModel
from public.base_view import BaseView, BaseDetailsView

group = {
    'id': fields.Integer(description='群组ID'),
    'group_name': fields.String(description='组名'),
    'desc': fields.String(description='群组描述'),
    'owner_id': fields.Integer(description='所有者ID'),
    'owner_name': fields.String(description='所有者姓名'),
}

group_marshal = MarshalModel('group', system_oauth_ns)


class GroupDetailView(BaseDetailsView):
    url = '/group/detail'

    def __init__(self, *args, **kwargs):
        super(GroupDetailView, self).__init__(*args, **kwargs)
        self.table_orm = SystemGroup
        self.table_schema = SystemGroupSchema

    @handle_api_error()
    @system_oauth_ns.doc(description="获取群组详情")
    @system_oauth_ns.expect(group_marshal.detail_request())
    @system_oauth_ns.marshal_with(group_marshal.detail_response(group))
    def get(self):
        """获取群组详情"""
        return super(GroupDetailView, self).get()


class GroupView(BaseView):
    url = '/group'

    def __init__(self, *args, **kwargs):
        super(GroupView, self).__init__(*args, **kwargs)
        self.table_orm = SystemGroup
        self.table_schema = SystemGroupSchema

    def handle_response_data(self):
        permission_ids = self.form_data.get('permission_ids')
        if request.method in ['POST', 'PUT'] and permission_ids:
            for permission_id in permission_ids:
                obj = SystemGroupPermissionRelation(permission_id=permission_id, group_id=self.table_id)
                db.session.add(obj)
                db.session.commit()

        elif request.method == 'DELETE':
            with db.auto_commit():
                db.session.query(SystemGroupPermissionRelation).filter(
                    SystemGroupPermissionRelation.group_id == self.table_id).update({'active': 0})

    @handle_api_error()
    @system_oauth_ns.doc(description='获取群组列表')
    @system_oauth_ns.expect(group_marshal.list_request(group))
    @system_oauth_ns.marshal_with(group_marshal.list_response(group))
    def get(self):
        """获取群组列表"""
        return super(GroupView, self).get()

    @handle_api_error()
    @system_oauth_ns.doc(description='创建群组')
    @system_oauth_ns.expect(group_marshal.create_request(group))
    @system_oauth_ns.marshal_with(group_marshal.create_response())
    def post(self):
        """创建群组"""
        return super(GroupView, self).post()

    @handle_api_error()
    @system_oauth_ns.doc(description='修改群组')
    @system_oauth_ns.expect(group_marshal.update_request(group))
    @system_oauth_ns.marshal_with(group_marshal.update_response())
    def put(self):
        """修改群组"""
        return super(GroupView, self).put()

    @handle_api_error()
    @system_oauth_ns.doc(description='删除群组')
    @system_oauth_ns.expect(group_marshal.delete_request())
    @system_oauth_ns.marshal_with(group_marshal.delete_response())
    def delete(self):
        """删除群组"""
        return super(GroupView, self).delete()
